<?php

session_start();
class cart extends CI_Controller {

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     * 		http://example.com/index.php/welcome
     * 	- or -  
     * 		http://example.com/index.php/welcome/index
     * 	- or -
     * Since this controller is set as the default controller in 
     * config/routes.php, it's displayed at http://example.com/
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see http://codeigniter.com/user_guide/general/urls.html
     */
    private $_configs = null;

    function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->library('smarty3', '', 'smarty');
        include APPPATH . 'libraries/defu.php';
        $this->load->model('pushclimb/tour_model');
        $this->tour_model=new tour_model();
    }
    function paypalCancel(){
        
    }
    function paypalReturn(){
        
    }
    function paypalNotify(){
        
    }
    function index() {
        $config=array(
            "currencyCode"  =>"USD",
            "paypalID"      =>"khsale_1352425073_biz@gmail.com",
            "returnUrl"     =>base_url("cart/paypalReturn"),
            "notifyUrl"     =>base_url("cart/paypalNotify"),
            "cancelUrl"     =>base_url("cart/paypalCancel"),
            "paypalsandbox" =>true,
            "paypalhttps"   =>true
        );
        if(isset($_SESSION["CART"]["PR"]))foreach ($_SESSION["CART"]["PR"] as $pr){
            $pecent=1;
            if (isset($_SESSION["Discounts"]) and 
                    !strpos(",".$_SESSION["Discounts"].",",$pr->ProductID )===false
                    and isset($_SESSION["push"]["user"])
                    ){
                $pecent=0.9;
            }
            $jcart[]=array(
                "id"=>$pr->ProductID,
                "name"=>$pr->ProductName,
                "price"=>$pr->UnitPrice*$pecent,
                "qty"=>$pr->NUM
            );
            
        }
        if(isset($_SESSION["CART"]["PK"]))foreach ($_SESSION["CART"]["PK"] as $pk){
            $jcart[]=array(
                "id"=>$pk->ID,
                "name"=>$pk->PackageName,
                "price"=>$pk->UnitPrice,
                "qty"=>$pk->NUM
            );
        }
        // Build the query string
        $params=array(
            "cmd"           =>  "_cart",
            "upload"        =>  "1",
            "charset"       =>  "utf-8",
            "currency_code" =>  $config['currencyCode'],
            "business"      =>  $config['paypalID'],
            "return"        =>  $config['returnUrl'],
            "notify_url"    =>  $config['notifyUrl'],
        );
        
        $count = 1;
        foreach ($jcart as $item) {
            $params['item_number_' . $count]    = $item['id'];
            $params['item_name_' . $count]    = $item['name'];
            $params['amount_' . $count]    = $item['price'];
            $params['quantity_' . $count]    = $item['qty'];
            // Increment the counter
            ++$count;
        }

        unset($_SESSION["CART"]);
        
        // Add the sandbox subdomain if necessary
        $sandbox = '';
        if ($config['paypalsandbox'] === true) {
            $sandbox = '.sandbox';
        }
        // Use HTTPS by default
        $protocol = 'https://';
        if ($config['paypalhttps'] == false) {
            $protocol = 'http://';
        }
        // Send the visitor to PayPal https://www.sandbox.paypal.com/cgi-bin/webscr
        
        $url=$protocol . 'www' . $sandbox . '.paypal.com/cgi-bin/webscr?' . http_build_query($params);
        //echo "Payment width <a href='$url'>Paypal</a> or <a href=''>Order</a>";
        @header("Location: $url");
        
    }
    function buytour($alias=""){
        $tmp=$this->tour_model->getByAlias($alias);
        if(isset($tmp[0]))$tour=$tmp[0];
        else            show_404 ();
        $config=array(
            "currencyCode"  =>"USD",
            "paypalID"      =>"khsale_1352425073_biz@gmail.com",
            "returnUrl"     =>base_url("cart/paypalReturn"),
            "notifyUrl"     =>base_url("cart/paypalNotify"),
            "cancelUrl"     =>base_url("cart/paypalCancel"),
            "paypalsandbox" =>true,
            "paypalhttps"   =>true
        );
        $params=array(
            "cmd"           =>  "_cart",
            "upload"        =>  "1",
            "charset"       =>  "utf-8",
            "currency_code" =>  $config['currencyCode'],
            "business"      =>  $config['paypalID'],
            "return"        =>  $config['returnUrl'],
            "notify_url"    =>  $config['notifyUrl'],
            
            "item_number_1"  =>  $tour->ID,
            "item_name_1"    =>  $tour->Title,
            "amount_1"       =>  $tour->UnitPrice,
            "quantity_1"     =>  1,
        );
        
        // Add the sandbox subdomain if necessary
        $sandbox = '';
        if ($config['paypalsandbox'] === true) {
            $sandbox = '.sandbox';
        }
        // Use HTTPS by default
        $protocol = 'https://';
        if ($config['paypalhttps'] == false) {
            $protocol = 'http://';
        }
        // Send the visitor to PayPal https://www.sandbox.paypal.com/cgi-bin/webscr
        
        $url=$protocol . 'www' . $sandbox . '.paypal.com/cgi-bin/webscr?' . http_build_query($params);
        //echo "Payment width <a href='$url'>Paypal</a> or <a href=''>Order</a>";
        
        @header("Location: $url");
    }
}

?>